package com.goods.business.service.imp;

import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/10 17:30 Administrator
 * @Description
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;
    /**
     * 查询物资去处
     * @param map
     * @return
     */
    @Override
    public PageVO<ConsumerVO> findConsumerList(Map map) {
        List<Consumer> consumerList=consumerMapper.findConsumerList(map);
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());

        List<ConsumerVO> consumerVOList = consumerList.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

        //总条数
        int total=consumerVOList.size();
        //从第几页开始显示
        int size=(pageNum-1)*pageSize+pageSize;
        List<ConsumerVO> consumerVOList1=consumerVOList.subList((pageNum-1)*pageSize,size>total?total:size);
        PageVO<ConsumerVO> consumerVOPageVO = new PageVO<>(total,consumerVOList1);
        return consumerVOPageVO;
    }

    /**
     * 添加物资去处
     * @param consumerVO
     */
    @Override
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        BeanUtils.copyProperties(consumerVO,consumer);
        consumerMapper.insert(consumer);
    }

    /**
     * 根据ID查询物资出去用作数据回显
     * @param id
     * @return
     */
    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        return consumerVO;
    }

    /**
     * 修改数据
     * @param id
     * @param consumerVO
     */
    @Override
    public void update(Long id, ConsumerVO consumerVO) {
        Consumer consumer1 = consumerMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(consumerVO,consumer1);
        consumer1.setCreateTime(new Date());
        consumer1.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer1);
    }

    /**
     * 根据id 删除物资去处
     * @param id
     */
    @Override
    public void delete(Long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }

    /**
     * 物资去向
     * @return
     */
    @Override
    public List<ConsumerVO> findAll() {
        List<Consumer> consumerList = consumerMapper.selectAll();
        List<ConsumerVO> consumerVOList = consumerList.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return consumerVOList;
    }
}
